package cn.gmph.blog.admin.service.post.dao.helper;

import java.util.Map;

import org.apache.ibatis.jdbc.SQL;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.gmph.blog.admin.service.post.entity.CommentsDO;
import cn.gmph.blog.admin.web.dto.CommentQueryDTO;
import cn.gmph.core.utils.StringUtils;

public class CommentsProvider {

    private Logger              logger     = LoggerFactory.getLogger(this.getClass());

    private static final String TABLE_NAME = "wp_comments";

    public String update(Map<String, Object> params) {
        CommentsDO comments = (CommentsDO) params.get("comments");
        return new SQL() {
            {
                UPDATE(TABLE_NAME);
                if (comments.getCommentId() != null) {
                    SET("comment_id=#{comments.commentId}");
                }
                if (comments.getCommentPostId() != null) {
                    SET("comment_post_id=#{comments.commentPostId}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthor())) {
                    SET("comment_author=#{comments.commentAuthor}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorEmail())) {
                    SET("comment_author_email=#{comments.commentAuthorEmail}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorUrl())) {
                    SET("comment_author_url=#{comments.commentAuthorUrl}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorIp())) {
                    SET("comment_author_ip=#{comments.commentAuthorIp}");
                }
                if (comments.getCommentDate() != null) {
                    SET("comment_date=#{comments.commentDate}");
                }
                if (comments.getCommentDateGmt() != null) {
                    SET("comment_date_gmt=#{comments.commentDateGmt}");
                }
                if (StringUtils.isNotBlank(comments.getCommentContent())) {
                    SET("comment_content=#{comments.commentContent}");
                }
                if (comments.getCommentKarma() != null) {
                    SET("comment_karma=#{comments.commentKarma}");
                }
                if (StringUtils.isNotBlank(comments.getCommentApproved())) {
                    SET("comment_approved=#{comments.commentApproved}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAgent())) {
                    SET("comment_agent=#{comments.commentAgent}");
                }
                if (StringUtils.isNotBlank(comments.getCommentType())) {
                    SET("comment_type=#{comments.commentType}");
                }
                if (comments.getCommentParent() != null) {
                    SET("comment_parent=#{comments.commentParent}");
                }
                if (comments.getUserId() != null) {
                    SET("user_id=#{comments.userId}");
                }
                WHERE("id = #{comments.id}");
            }
        }.toString();
    }

    public String pageList(Map<String, Object> params) {
        CommentsDO comments = (CommentsDO) params.get("comments");
        Integer pageNum = (Integer) params.get("pageNum");
        Integer pageSize = (Integer) params.get("pageSize");
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {
            {
                SELECT("*");
                FROM(TABLE_NAME);
                if (comments.getCommentId() != null) {
                    WHERE("comment_id=#{comments.commentId}");
                }
                if (comments.getCommentPostId() != null) {
                    WHERE("comment_post_id=#{comments.commentPostId}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthor())) {
                    WHERE("comment_author=#{comments.commentAuthor}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorEmail())) {
                    WHERE("comment_author_email=#{comments.commentAuthorEmail}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorUrl())) {
                    WHERE("comment_author_url=#{comments.commentAuthorUrl}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorIp())) {
                    WHERE("comment_author_ip=#{comments.commentAuthorIp}");
                }
                if (comments.getCommentDate() != null) {
                    WHERE("comment_date=#{comments.commentDate}");
                }
                if (comments.getCommentDateGmt() != null) {
                    WHERE("comment_date_gmt=#{comments.commentDateGmt}");
                }
                if (StringUtils.isNotBlank(comments.getCommentContent())) {
                    WHERE("comment_content=#{comments.commentContent}");
                }
                if (comments.getCommentKarma() != null) {
                    WHERE("comment_karma=#{comments.commentKarma}");
                }
                if (StringUtils.isNotBlank(comments.getCommentApproved())) {
                    WHERE("comment_approved=#{comments.commentApproved}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAgent())) {
                    WHERE("comment_agent=#{comments.commentAgent}");
                }
                if (StringUtils.isNotBlank(comments.getCommentType())) {
                    WHERE("comment_type=#{comments.commentType}");
                }
                if (comments.getCommentParent() != null) {
                    WHERE("comment_parent=#{comments.commentParent}");
                }
                if (comments.getUserId() != null) {
                    WHERE("user_id=#{comments.userId}");
                }
                ORDER_BY("id desc limit " + start + ", " + limit);
            }
        }.toString();
    }

    public String pageListCount(Map<String, Object> params) {
        CommentsDO comments = (CommentsDO) params.get("comments");
        return new SQL() {
            {
                SELECT("count(1)");
                FROM(TABLE_NAME);
                if (comments.getCommentId() != null) {
                    WHERE("comment_id=#{comments.commentId}");
                }
                if (comments.getCommentPostId() != null) {
                    WHERE("comment_post_id=#{comments.commentPostId}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthor())) {
                    WHERE("comment_author=#{comments.commentAuthor}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorEmail())) {
                    WHERE("comment_author_email=#{comments.commentAuthorEmail}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorUrl())) {
                    WHERE("comment_author_url=#{comments.commentAuthorUrl}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAuthorIp())) {
                    WHERE("comment_author_ip=#{comments.commentAuthorIp}");
                }
                if (comments.getCommentDate() != null) {
                    WHERE("comment_date=#{comments.commentDate}");
                }
                if (comments.getCommentDateGmt() != null) {
                    WHERE("comment_date_gmt=#{comments.commentDateGmt}");
                }
                if (StringUtils.isNotBlank(comments.getCommentContent())) {
                    WHERE("comment_content=#{comments.commentContent}");
                }
                if (comments.getCommentKarma() != null) {
                    WHERE("comment_karma=#{comments.commentKarma}");
                }
                if (StringUtils.isNotBlank(comments.getCommentApproved())) {
                    WHERE("comment_approved=#{comments.commentApproved}");
                }
                if (StringUtils.isNotBlank(comments.getCommentAgent())) {
                    WHERE("comment_agent=#{comments.commentAgent}");
                }
                if (StringUtils.isNotBlank(comments.getCommentType())) {
                    WHERE("comment_type=#{comments.commentType}");
                }
                if (comments.getCommentParent() != null) {
                    WHERE("comment_parent=#{comments.commentParent}");
                }
                if (comments.getUserId() != null) {
                    WHERE("user_id=#{comments.userId}");
                }
            }
        }.toString();
    }

    public String selectForPage(Map<String, Object> params) {
        CommentQueryDTO query = (CommentQueryDTO) params.get("comments");
        Integer pageNum = query.getPageNo();
        Integer pageSize = query.getPageSize();
        if (pageNum == null || pageNum == 0) {
            pageNum = 1;
        }
        if (pageSize == null || pageSize == 0) {
            pageSize = 20;
        }
        int start = (pageNum - 1) * pageSize;
        int limit = pageSize;
        return new SQL() {
            {
                SELECT("SQL_CALC_FOUND_ROWS comment_ID");
                FROM(TABLE_NAME);
                if (query.getStatuses() != null) {
                    WHERE("comment_approved in ( " + query.getStatuses() + " )");
                }
                if (query.getPostId() != null) {
                    WHERE("comment_post_ID = #{comments.postId}");
                }
                if (query.getSortkey() != null) {
                    ORDER_BY(query.getSortkey() + " limit " + start + ", " + limit);
                } else {
                    ORDER_BY("comment_ID DESC  limit " + start + ", " + limit);
                }
            }
        }.toString();
    }
}
